Policies_Ui_ClassLimits = Class.create({
    event_node_ : '',
    row_cnt_ : 0,
    arr_response_ : '',
    initialize: function()
    {
        if($('classwidth')===null)
            return false;
        $('classwidth').observe('change', this.changeClassLimits.bindAsEventListener(this));
        $('classification').observe('click', this.toggleOptions.bindAsEventListener(this));
    },
    changeClassLimits: function()
    {
        if($('user_classes').checked)
        {
            element = $('reset_class_limits');
            new Ajax.Request(element.href, {
                method: "get",
                parameters : { 'mode' : 'ajax', 'classwidth' : $F('classwidth')},
                onSuccess: this.updateClasses.bind(this),
                onLoading : $('classwidth').writeAttribute('disabled', 'disabled')
                //onFailure : function(){window.location.href = this.event_node_}.bind(this),
            });
        }
    },
    updateClasses : function(response)
    {
        var class_length, tmp_div, insert_string;
        this.arr_response_ = response.responseJSON;
        class_length = parseInt($F('classwidth'));
        if((class_length+1) == this.arr_response_.length)
        {
            $$('#equal_classes_option_values input').each(function(element){
                element.remove();
            });
            $$('#equal_classes_option_values span').each(function(element){
                element.remove();
            });
            insert_string = '';
            for(cnt=0; cnt<this.arr_response_.length; ++cnt)
            {
                insert_string += '<input type="text" name="arr_classlimits[]" value="' +this.arr_response_[cnt]+'" />';
                if(cnt < class_length)
                    insert_string += '<span>&lt;</span>';
            }
            $('equal_classes_option_values').insert({ 'top' : insert_string});
        }
        else
            alert('Es gab leider Probleme');    
        $('classwidth').writeAttribute('disabled', false);
        //this.row_cnt_ = 0;
    },
    toggleOptions : function(event)
    {
        element = event.findElement('input');
        if(element != null && element.hasAttribute('id') && element.readAttribute('id') != 'classwidth')
        { 
            if(element.readAttribute('id') == 'user_classes')
            {
                this.changeClassLimits();
                $('equal_classes_options').show();
            }
            else
                $('equal_classes_options').hide();
        }
        element = event.findElement('a');
        if(element != null && element.hasAttribute('id') && element.readAttribute('id') == 'reset_class_limits')
        {
            event.stop();
            this.getDefaultValues(element);        
        }
    },
    getDefaultValues : function(element)
    {
        this.event_node_ = element.href;
        new Ajax.Request(element.href, {
            method: "get",
            parameters : { 'mode' : 'ajax'},
            onSuccess: this.updateDefaultValues.bind(this),
            onFailure : function(){window.location.href = this.event_node_}.bind(this)
        });
    },
    updateDefaultValues : function(response)
    {
        this.arr_response_ = response.responseJSON;
        $$('#equal_classes_option_values input').each(function(element){
            if(this.row_cnt_ < this.arr_response_.length)
                element.value = this.arr_response_[this.row_cnt_];
            ++this.row_cnt_
        }.bind(this));
        this.row_cnt_ = 0;
    }
});
